package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;


/**
 * 04-ServletConfig对象
 */
public class Demo02Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();

        //①获取Servlet初始化参数
        String username = servletConfig.getInitParameter("username");
        System.out.println("username = " + username);
        String password = servletConfig.getInitParameter("password");
        System.out.println("password = " + password);
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String initParameterName = initParameterNames.nextElement();
            String initParameterValue = servletConfig.getInitParameter(initParameterName);

            System.out.println("initParameterName = " + initParameterName + " , initParameterValue = " + initParameterValue);

        }
        //②获取ServletContext对象
        ServletContext servletContext = servletConfig.getServletContext();
        System.out.println("servletContext = " + servletContext);
        ServletContext servletContext2 = getServletContext();
        System.out.println(servletContext == servletContext2);

        //③获取Servlet名称
        String servletName = servletConfig.getServletName();
        System.out.println("servletName = " + servletName);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
